<?php
	class Tintuc extends CI_Controller
    {
        public function __construct() {
            parent::__construct();
            $this->load->helper(array('url', 'text'));
            $this->load->library('useronline'); 
        }
        
        public function index()
        {
            $this->load->model('Nkdefault', 'default');
            $this->load->model('admin/managebaiviet','baiviet');                
            $page = $this->input->get('page');
            $number = $this->input->get('number');
            if($number == '') $number = 0;
            $madanhmuc = $this->input->get('madanhmuc');
            $madanhmuc_cha = $this->input->get('madanhmuc_cha');            
            $temp['danhmuc']['menu'] = $this->default->loadDanhMuc();            
            $temp['currentPage'] = $number;           
            $temp['title'] = $page;
            $temp['template'] = 'tintuc';
            $temp['data']['info'] = $page;
            $where = array('madanhmuc' => $madanhmuc, 'madanhmuc_cha' => $madanhmuc_cha, 'hienthi' => '1');
            //get total rows in table
            $totalRows = $this->default->countRowsResults('tintuc', $where);   
            $temp['danhmuc']['currentItem'] = $madanhmuc_cha;
            $temp['madanhmuc'] = $madanhmuc;
            $temp['madanhmuc_cha'] = $madanhmuc_cha;           
            //get total page number that created
            //10 is maximum records will display in page. 
            $totalPage = ceil($totalRows / 10);
            $temp['totalPage'] = $totalPage;
            //loaded records from table follow page number.
            $start = 0;            
            if($number > 0) $start = ($number * 10) + 1;
            $content = $this->baiviet->LoadBaiViet($where, 10, $start);
            $arrResult = array();
            foreach($content->result_array() as $row)
            {
                $noidungbaiviet = strip_tags(word_limiter($row['noidungbaiviet_viet'], 20));
                $subResult = array('tieude' => $row['tieude'],
                                    'id' => $row['id'],
                                    'ngayvietbai' => $row['ngayvietbai'],
                                    'noidungbaiviet_viet' => $noidungbaiviet,
                                    'imagelink' => $row['imagelink'],
                                    'madanhmuc' => $row['madanhmuc'],
                                    'madanhmuc_cha'=>$row['madanhmuc_cha']);
                array_push($arrResult, $subResult);
            }
            $temp['data']['content'] = $arrResult;
            $temp['online'] = $this->useronline->countOnline();
            $thongbao =$this->LoadThongBao();
            if(!empty($thongbao)) $temp['thongbao'] = $thongbao->result_array();
            $this->load->view('template/layout', $temp);
        }
        
        public function view()
        {
            $this->load->model('Nkdefault', 'default');
            $this->load->model('admin/managebaiviet','baiviet');
            $madanhmuc = $this->input->get('madanhmuc');
            $madanhmuc_cha = $this->input->get('madanhmuc_cha');
            $tieude = $this->input->get('tieude');
            $id = $this->input->get('id');
            $temp['danhmuc']['menu'] = $this->default->loadDanhMuc();
            $temp['danhmuc']['currentItem'] = $madanhmuc_cha;
            $temp['title'] = $tieude;
            $temp['template'] = 'viewtintuc';
            $temp['data']['info'] = $tieude;
            $where = array('id' => $id, 'hienthi' => '1');
            $content = $this->baiviet->LoadBaiViet($where);            
            $temp['data']['content'] = $content->result_array();
            //Lay cac bai da dang cung danh muc
            $where1 = array('madanhmuc' => $madanhmuc, 'madanhmuc_cha' => $madanhmuc_cha, 'hienthi' => '1');            
            $cacbaidadang = $this->baiviet->LoadBaiViet($where1, $id, 10, 0);  
            $temp['data']['cacbaidadang'] = $cacbaidadang->result_array();
            $temp['online'] = $this->useronline->countOnline();
            $thongbao =$this->LoadThongBao();
            if(!empty($thongbao)) $temp['thongbao'] = $thongbao->result_array();
            $this->load->view('template/layout', $temp);
        }  
        
        private function LoadThongBao()
        {
            $this->load->model('admin/managethongbao', 'thongbao');  
            //load thong bao
            $thongbao = $this->thongbao->LoadThongBao(null, null, 1, 0);
            return $thongbao; 
        }
    }
?>